package com.gemfire.onlineretailer.domain;

/**
 * This class represents one Account Head like Salary Expenses, Traveling Expenses , Sales Income, Land and Machinery . 
 * These Account Heads form a hierarchical system  in the Organization's Chart of Accounts. Financial transaction that affect the lowest account heads are entered 
 * into the system in the form of a  Payment Voucher / Receipt Voucher / Contra Adjustment Vouchers. 
 * These voucher documents normally has two minimum account heads one to be credited and another to be debitted.
 *  Consequently the balances of these accounts heads go up or down depending on the transactions. 
 *  These lowest level accounts head are also tied to the  Higher level account heads from which the Organization's Balance Sheet and 
 *  Income / Expenditure statement reports are produced.
 *  
 * @author Binit Datta
 */

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import com.gemstone.gemfire.DataSerializable;

public class AccountHeads implements DataSerializable {
	  private static final long serialVersionUID = 1L;
	  
	  
	  public static final long ACCOUNT_HEAD_1 = 10000001;
	  public static final long ACCOUNT_HEAD_2 = 10000002;

	  public static final long ACCOUNT_HEAD_3 = 10000003;
	  public static final long ACCOUNT_HEAD_4 = 10000004;
	  public static final long ACCOUNT_HEAD_5 = 10000005;
	  
	  public static final String ACCOUNT_HEAD_1_CODE = "10000001";
	  public static final String ACCOUNT_HEAD_2_CODE = "10000002";

	  public static final String ACCOUNT_HEAD_3_CODE = "10000003";
	  public static final String ACCOUNT_HEAD_4_CODE = "10000004";
	  public static final String ACCOUNT_HEAD_5_CODE = "10000005";
	  
	  
	  private long accountId;
	  private String accountCode;
	  private int accountType;
	  private String accountName;
	  private double accountBalance;
	  
	  public AccountHeads(){
		  
	  }
	  
	public long getAccountId() {
		return accountId;
	}
	public void setAccountId(long accountId) {
		this.accountId = accountId;
	}
	public String getAccountCode() {
		return accountCode;
	}
	public void setAccountCode(String accountCode) {
		this.accountCode = accountCode;
	}
	public int getAccountType() {
		return accountType;
	}
	public void setAccountType(int accountType) {
		this.accountType = accountType;
	}
	public String getAccountName() {
		return accountName;
	}
	public void setAccountName(String accountName) {
		this.accountName = accountName;
	}
	public double getAccountBalance() {
		return accountBalance;
	}
	public void setAccountBalance(double accountBalance) {
		this.accountBalance = accountBalance;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	  
	
	public static AccountHeads createInstance(long accountId){
		
		/*
		 * Use accountHeadId to read from DB or from other source, Hardcode for now
		 */
		
		AccountHeads accountHead = new AccountHeads();
		
		if (accountId == ACCOUNT_HEAD_1){
			System.out.println("AC1 :"+ACCOUNT_HEAD_1);
			accountHead.setAccountId(accountId);
			accountHead.setAccountCode(ACCOUNT_HEAD_1_CODE);
			accountHead.setAccountType(AccountType.ASSET.getCode()); 
			accountHead.setAccountName("Land and Buildings");
			accountHead.setAccountBalance(50000000d);
		} 
		else if (accountId == ACCOUNT_HEAD_2){
			System.out.println("AC2 :"+ACCOUNT_HEAD_2);

			accountHead.setAccountId(accountId);
			accountHead.setAccountCode(ACCOUNT_HEAD_2_CODE);
			accountHead.setAccountType(AccountType.EXPENSE.getCode()); 
			accountHead.setAccountName("Salaries Paid");
			accountHead.setAccountBalance(500000d);

		} 
		else if (accountId == ACCOUNT_HEAD_3){
			System.out.println("AC3 :"+ACCOUNT_HEAD_3);

			accountHead.setAccountId(accountId);
			accountHead.setAccountCode(ACCOUNT_HEAD_3_CODE);
			accountHead.setAccountType(AccountType.INCOME.getCode()); 
			accountHead.setAccountName("Quarterly Sales");
			accountHead.setAccountBalance(5000000d);

		} 
		else if (accountId == ACCOUNT_HEAD_4){
			System.out.println("AC4 :"+ACCOUNT_HEAD_4);

			accountHead.setAccountId(accountId);
			accountHead.setAccountCode(ACCOUNT_HEAD_4_CODE);
			accountHead.setAccountType(AccountType.LIABILITIES.getCode()); 
			accountHead.setAccountName("Accounts Payable for Raw Materials Purchase");
			accountHead.setAccountBalance(20000d);

		}
		
		if (accountHead == null){
			System.out.println("NULL :");
		}
		else {
			System.out.println("NOT NULL :");

		}
		return accountHead;
		
	}
	
	 @Override
	  public String toString() {
	    
		 StringBuffer buff = new StringBuffer();
		 
		 buff.append(" Account Id :"+accountId);
		 buff.append(" Account Code :"+accountCode);
		 buff.append(" Account  Type :"+accountType);
		 buff.append(" Account Name :"+accountName);
		 buff.append(" Account Balance :"+accountBalance);
	
		 return buff.toString();
	  }
	@Override
	public void fromData(DataInput in) throws IOException,
			ClassNotFoundException {
		this.accountId = in.readLong();
		this.accountCode = in.readUTF();
		this.accountType = in.readInt();
		this.accountName  = in.readUTF();
		this.accountBalance = in.readDouble();
		
	}
	@Override
	public void toData(DataOutput out) throws IOException {
		 out.writeLong(this.accountId);
		 out.writeUTF(this.accountCode);
		 out.writeInt(this.accountType);
		 out.writeUTF(this.accountName);
		 out.writeDouble(this.accountBalance);
	}
	  

}
